<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section class="tb-kv-map flex flex-col" [formGroup]="kvListFormGroup">
  <label translate class="tb-title">{{ titleText() }}</label>
  @for (keyValControl of keyValsFormArray().controls; track keyValControl) {
    <div class="flex flex-row items-center justify-start gap-2" formArrayName="keyVals">
      <mat-form-field floatLabel="always" hideRequiredMarker class="mat-block flex-1"
        subscriptSizing="{{ subscriptSizing() }}" appearance="outline">
        @if ($index === 0) {
          <mat-label translate>integration.header</mat-label>
        }
        <input [formControl]="keyValControl.get('key')" matInput required
          placeholder="{{ (keyPlaceholderText() ? keyPlaceholderText() : 'key-val.key') | translate }}"/>
      </mat-form-field>
      <mat-form-field floatLabel="always" hideRequiredMarker class="mat-block flex-1"
        subscriptSizing="{{ subscriptSizing() }}" appearance="outline">
        @if ($index === 0) {
          <mat-label translate>key-val.value</mat-label>
        }
        <input [formControl]="keyValControl.get('value')" matInput [required]="isValueRequired()"
          placeholder="{{ (valuePlaceholderText() ? valuePlaceholderText() : 'key-val.value') | translate }}"/>
      </mat-form-field>
      <button mat-icon-button color="primary"
        style="top: 4px; align-self: baseline"
        [class.!hidden]="disabled"
        type="button"
        (click)="removeKeyVal($index)"
        [disabled]="isLoading$ | async"
        matTooltip="{{ 'key-val.remove-entry' | translate }}"
        matTooltipPosition="above">
        <mat-icon>close</mat-icon>
      </button>
    </div>
  }
  <span [class]="{
          'disabled': disabled,
          '!hidden': keyValsFormArray().length
        }"
  class="no-data-found flex items-center justify-center" translate>{{noDataText() ? noDataText() : 'key-val.no-data'}}</span>
  <div style="margin-top: 8px;">
    @if (isStrokedButton()) {
      <button mat-button mat-stroked-button color="primary"
        [class.!hidden]="disabled || (isSingleMode && keyValsFormArray().length)"
        [disabled]="isLoading$ | async"
        (click)="addKeyVal()"
        type="button" style="width: 100%"
        matTooltip="{{ 'key-val.add-entry' | translate }}"
        matTooltipPosition="above">
        {{ 'key-val.add-header' | translate }}
      </button>
    }
  </div>
</section>
